﻿/*
 * Copyright (c) 2015-2016 Constantijn Evenhuis (blueprints@const.nl)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
using Blueprints.Resolution;
using System;
using System.ComponentModel;
using System.Reflection;

namespace Blueprints.Defaults
{
    /// <summary>
    /// The default property descriptor. 
    /// </summary>
    internal sealed class ReflectionPropertyDescriptor : IPropertyDescriptor
    {
        private readonly object defaultValue;
        private readonly bool hasDefaultValue;
        private readonly PropertyInfo propertyInfo;

        /// <summary>
        /// Creates a new PropertyInfoDescriptor.
        /// </summary>
        /// <param name="propertyInfo">The property to describe.</param>
        public ReflectionPropertyDescriptor(PropertyInfo propertyInfo)
        {
            this.propertyInfo = propertyInfo;
            Name = this.propertyInfo.Name;
            Type = this.propertyInfo.PropertyType;

            DefaultValueAttribute attribute = propertyInfo.GetCustomAttribute<DefaultValueAttribute>();
            if (attribute == null)
            {
                hasDefaultValue = false;
                defaultValue = null;
            }
            else
            {
                hasDefaultValue = true;
                defaultValue = attribute.Value;
            }
        }

        /// <summary>
        /// Gets the name of the property.
        /// </summary>
        public string Name
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the type of property.
        /// </summary>
        public Type Type
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the value of the property for <paramref name="instance"/>.
        /// </summary>
        /// <param name="instance">The instance which contains the property.</param>
        /// <returns>The value of the property.</returns>
        public object GetValue(object instance)
        {
            return propertyInfo.GetValue(instance);
        }

        /// <summary>
        /// Sets the value of the property for <paramref name="instance"/>.
        /// </summary>
        /// <param name="instance">The instance which contains the property.</param>
        /// <param name="value">The value to set.</param>
        public void SetValue(object instance, object value)
        {
            propertyInfo.SetValue(instance, value);
        }

        /// <summary>
        /// Indicates whether <paramref name="value"/> should be serialized for <paramref name="instance"/>. Omitting
        /// default values yields smaller files. 
        /// </summary>
        /// <param name="instance">The instance which contains the property.</param>
        /// <param name="value">The property value.</param>
        /// <returns>True if the value should be serialized, false otherwise.</returns>
        public bool ShouldSerializeValue(object instance, object value)
        {
            if (!hasDefaultValue)
            {
                return true;
            }

            if (Type.GetTypeInfo().IsValueType)
            {
                return !value.Equals(defaultValue);
            }
            else
            {
                return value != defaultValue;
            }
        }
    }
}